package com.zzptc.zy.myapplication.adapter;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zzptc.zy.myapplication.R;
import com.zzptc.zy.myapplication.databinding.RecipeItemBinding;
import com.zzptc.zy.myapplication.entity.ResultsBean;

import org.w3c.dom.Text;

import java.util.List;

public class MyRecyclerviewAdapter extends RecyclerView.Adapter<MyRecyclerviewAdapter.ViewHolder>{ //ait+回车 3次 都点第一个

    private List<ResultsBean>reciptesList; //许多菜谱的表单  //形式参数

    private Context context;

    public MyRecyclerviewAdapter(List<ResultsBean> reciptesList, Context context) {  // AIT+Inset 这是用的构造方法
        this.reciptesList = reciptesList;
        this.context = context;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //创建小页面
        //View inflate = View.inflate(context, R.layout.recipe_item, null);//这是以前的方法
        RecipeItemBinding binding= DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                                    R.layout.recipe_item,parent,false);

        return new ViewHolder(binding);
    }

    @Override //绑定数据 onbind
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //将数据取出
        ResultsBean resultsBean = reciptesList.get(position);//后半节+var+回车

        holder.recipeItemBinding.tvTitle.setText(resultsBean.getTitle());//Viewholderd的数据
        Glide.with(context)
                .load(resultsBean.getCover())
                .into(holder.recipeItemBinding.ivPic);//从网址上获取图片的数据

    }





    @Override
    public int getItemCount() {
        return reciptesList.size();//return null=recoiptesList.size():
    }

    public class ViewHolder extends RecyclerView.ViewHolder { //也是ait+回车 点一个

        //private ImageView imageView;//所有的控件 以前的方法
        //private TextView   tv;
        //private TextView    textView;


        //增加成员属性
        private RecipeItemBinding recipeItemBinding;//代替以上控件

        public ViewHolder(@NonNull RecipeItemBinding reItemBinding) { //view =RecipeItemBinding

            super(reItemBinding.getRoot());//父类的构造方法
            this.recipeItemBinding=reItemBinding;


        }

    }
}
